<template>
 <div>
   <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addVisible=true">添加分类</el-button>
      </div>
   <el-table
 stripe
 border
      :data="cateList"
      style="width: 100%">
      <el-table-column
        type="index"
        label="序号"
        >
      </el-table-column>
      <el-table-column
        prop="cate_name"
        label="分类名称"
        >
      </el-table-column>
      <el-table-column
        prop="cate_alias"
        label="分类别名">
      </el-table-column>
      <el-table-column
        label="操作">
       <template v-slot='val'>
          <el-button type='primary' size="mini"   @click="editArtCate(val.row)">修改</el-button>
        <el-button type='danger' size="mini" @click="delArtCate(val.row.id)">删除</el-button>
       </template>
      </el-table-column>
    </el-table>
    </el-card>

    <el-dialog
  title="提示"
  :visible.sync="addVisible"
  width="35%"
  :close-on-click-modal='false'
  @closed='$refs.addForm.resetFields()'>
  <el-form :model="addForm" status-icon :rules="rules" ref="addForm" label-width="70px" class="demo-ruleForm">
  <el-form-item label="分类名称" prop="cate_name">
    <el-input type="text" v-model="addForm.cate_name" ></el-input>
  </el-form-item>
  <el-form-item label="分类别名" prop="cate_alias">
    <el-input type="text" v-model="addForm.cate_alias" ></el-input>
  </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click=" addVisible= false">取 消</el-button>
    <el-button type="primary" @click="updateCateList">确 定</el-button>
  </span>
</el-dialog>
 <el-dialog
  title="提示"
  :visible.sync="editVisible"
  width="35%"
  :close-on-click-modal='false'
  @closed='$refs.editForm.resetFields()'>
  <el-form :model="editForm" status-icon :rules="rules" ref="editForm" label-width="70px" class="demo-ruleForm">
  <el-form-item label="分类名称" prop="cate_name">
    <el-input type="text" v-model="editForm.cate_name" ></el-input>
  </el-form-item>
  <el-form-item label="分类别名" prop="cate_alias">
    <el-input type="text" v-model="editForm.cate_alias" ></el-input>
  </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click=" editVisible= false">取 消</el-button>
    <el-button type="primary" @click="hEdit">确 定</el-button>
  </span>
 </el-dialog>
 </div>
</template>

<script>
export default {
  name: 'ArtCate',
  data () {
    return {
      cateList: [],
      addVisible: false,
      editVisible: false,
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      editForm: {
        cate_name: '',
        cate_alias: ''
      },
      rules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名必须是1-10位的非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getcate()
  },
  methods: {
    async getcate () {
      const { data: res } = await this.$axios.get('/my/cate/list')
      console.log(res)
      if (res.code === 0) this.cateList = res.data
    },
    updateCateList () {
      this.$refs.addForm.validate(async valid => {
        if (!valid) return
        this.addVisible = false
        const { data: res } = await this.$axios.post('/my/cate/add', this.addForm)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.getcate()
      })
    },
    editArtCate (val) {
      if (val.id === 1 || val.id === 2) return this.$message.warning('默认不能修改')
      // console.log(val)
      this.editVisible = true
      // 深拷贝，避免一次修改全改了
      this.editForm = { ...val }
    },
    hEdit () {
      this.$refs.editForm.validate(async valid => {
        if (!valid) return
        this.editVisible = false
        const { data: res } = await this.$axios.put('/my/cate/info', this.editForm)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.getcate()
      })
    },
    delArtCate (id) {
      if (id === 1 || id === 2) return this.$message.warning('不许删除该项目')
      this.$confirm('真的要删除吗？', '提示', { type: 'warning' }).then(async () => {
        const { data: res } = await this.$axios.delete('/my/cate/del', { params: { id } })
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.getcate()
      }).catch(() => {

      })
    }
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
